home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 376-400 / disk_388 / free / env.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  62 lines

  1. /***************************************************************************
  2.  * env.c:    Environment variable functions.
  3.  *
  4.  * Part of...
  5.  *
  6.  *    FREE:    Display free space on your disk volumes.
  7.  *        Author:  Daniel Jay Barrett, barrett@cs.jhu.edu.
  8.  *
  9.  * This program is Freely Distributable.  Make all the copies you want
  10.  * and give them away.  Use this code in any way you like.
  11. ***************************************************************************/
  12.  
  13. #include "free.h"
  14.  
  15. /* Return the value of ENV: environment variable "variableName", if it
  16.  * exists. */
  17.  
  18. char *GetEnv(char *variableName)
  19. {
  20.     char envVar[ENV_NAME_LENGTH];
  21.     BPTR fileHandle;
  22.  
  23.     sprintf(envVar, "ENV:%s", variableName);
  24.     if ((fileHandle = Open(envVar, MODE_OLDFILE)) == 0)
  25.         return(NULL);
  26.     else
  27.         return(TheEnvValue(fileHandle));
  28. }
  29.  
  30.  
  31. /* Given a fileHandle on an environment variable, read the "file" and
  32.  * find the value of the variable.
  33.  * As far as I can tell, the value may have some garbage (control)
  34.  * characters after it.  I make sure to put a null ('\0') in place of
  35.  * the first such control character (if any).
  36.  *
  37.  * If the value is too large (>= BUFSIZ-1), then we can't handle it.
  38.  * Return NULL.
  39.  * Otherwise, terminate the value with a null and return a pointer to
  40.  * it.  We use a static array for this. */
  41.     
  42. char *TheEnvValue(BPTR fileHandle)
  43. {
  44.     static char envString[BUFSIZ];
  45.     int numChars;
  46.  
  47.     numChars = 0;
  48.  
  49.     if (Read(fileHandle, envString, (long)sizeof(envString)) > 0)
  50.     {
  51.         envString[BUFSIZ-1] = '\0';    /* Terminate for safety. */
  52.         while (envString[numChars] >= ' ')
  53.             numChars++;
  54.         if (numChars < sizeof(envString)-1)
  55.             envString[numChars] = '\0';
  56.         else
  57.             numChars = 0;
  58.     }
  59.     Close(fileHandle);
  60.     return(numChars ? envString : NULL);
  61. }
  62.